import { atom, selector } from 'recoil'
import { getTheQuarter } from '@/services/common'
import { isNullArray, isNullData, washYear } from '@/utils'
import { PublicEnum } from '@/@types'

export interface CurrentUser {
  id: number
  name: string
  avatar: string
  roles: string[]
  ward?: string
}

export const initialState: CurrentUser = {
  id: 0,
  name: '',
  avatar: '',
  roles: [],
}

export const userState = atom({
  key: 'userState',
  default: initialState,
})

// 季度数据
export const theQuarterData = selector({
  key: 'theQuarterData',
  get: async (): Promise<PublicEnum[]> => {
    const res = await getTheQuarter()
    // console.log('仓库数据🚀🚀🚀🚀', res)
    if (isNullData(res) && res.data && isNullArray(res.data)) {
      return washYear(res.data)
    }
    return []
  },
})
